// 轻提示语
const toast = msg => {
	setTimeout(()=>{
		uni.showToast({
			title: msg,
			icon: 'none',
			duration: 2000
		})
	},0)
};

// loading 状态
 const loading = (msg) => {
	 uni.showLoading({
	 	title: msg || '加载中...',
		icon: 'loading',
		mask: true
	 })
 };
 
 // l获取url参数
  const getUrlParam = (name,url) => {
 	 let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
 	 console.log(url,'urlurlurlurlurlurlurl');
	 let r = url.substr(1).match(reg);
	 
	 console.log(r,'rrrrrrrrrrr')
 	  if(r != null){
 	   return decodeURI(r[2]);
 	  } 
	  return '';

  };
 //关闭加载
 const hideLoading = ()=>{
	 uni.hideLoading();
 }
 
 
 // 打开登录弹窗
 const openLogin = () => {
	// gopage('/pages/login/index')
	uni.redirectTo({
		url:'/pages/login/index'
	})
	// uni.showModal({
	// 	title: '提示',
	// 	content: '您还没有登录或登录已失效，请先登录！',
	// 	confirmText: '登录',
	// 	confirmColor: '#3DA3F9',
	// 	success: res => {
	// 		console.log(res)
	// 		if(res.confirm) {
	// 			// uni.switchTab({
	// 			// 	url: '/pages/user/user'
	// 			// })
	// 			gotopage('/pages/login/index')
				
	// 		}else{
	// 			console.log("点击取消")
	// 			uni.navigateBack({
	// 				delta:1
	// 			})
	// 		}
	// 	}
	// })
 };

/**
 * 验证身份证号
 * @param card 身份证号
 * @returns {boolean} true: 合法 false: 不合法
 */
const regCard = card => {
	let reg = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
	if (reg.test(card)) {
		return true
	} else {
		return false;
	}
};

/**
 * 检测手机号
 * @param mobile 手机号
 * @returns {boolean}   合法为true
 */
const regMobile = mobile => {
	mobile = mobile || '';
	let reg = /^1[3456789]\d{9}$/;
	if (reg.test(mobile)) {
		return true;
	} else {
		return false;
	}
};

/**
 * 校验邮箱
 * @param mail 邮箱
 * @returns {boolean}   合法为true
 */
const regEmail = mail => {
	var reg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/; //正则表达式
	if (!reg.test(mail)) {
		return false;
	} else {
		return true;
	}
};

/**
 * 加密显示
 * @param text 需要加密的字符串  action_num  开始加密的位置  end_num 结束的不加密的字符串的位置
 * @returns {String}   加密字符串
 */
const replaceText = (text,action_num,end_num) => {
	let text_new = text.toString();
	text_new = text_new.substr(0, action_num) + '****' + text_new.substr(end_num);
    return text_new;
}

/**
 * gopage 页面跳转(navigateTo:保留当前页面)
 * @param {string} url 地址
 * @param {fun} successCallback 成功的回掉函数
 */
const gopage = (url, successCallback) => {
  uni.navigateTo({
    url: url,
    success: successCallback
  })
}
/**
 * gotopage 页面跳转(redirectTo，关闭所有页面)
 * @param {string} url 地址
 * @param {fun} successCallback 成功的回掉函数
 */
const gotopage = (url, successCallback) => {
  uni.reLaunch({
    url: url,
    success: successCallback
  })
}
/**
 * gotarpage 页面跳转(switchTab 跳转到tarbar页面)
 * @param {string} url 地址
 * @param {fun} successCallback 成功的回掉函数
 */
const gotarpage = (url, successCallback) => {
  uni.switchTab({
    url: url,
    success: successCallback
  })
}
/**
 * backpage 页面返回
 * @param {num} delta 页面层数
 * @param {fun} successCallback 成功的回掉函数
 */
const backpage = (delta, successCallback) => {
  uni.navigateBack({
    delta: delta || 1,
    success: successCallback
  })
}
/**
 * alertBack 有弹出框并页面返回
 * @param {string} title 提示标题
 * @param {num} time 时间
 * @param {string} url 页面地址
 */
const alertBack = (title, time, url) => {
  var time = time || 1000;
  uni.showToast({
    icon: "success",
    title: title || "成功",
    duration: time,
    success: () => {
      setTimeout(() => {
        if (url) {
          uni.redirectTo({ url: url, })
        } else {
          uni.navigateBack({ delta: 1 })
        }
      }, time)
    }
  })
}
/**
 * setCache 同步设置缓存
 * @param {string} key key值
 * @param {string} value 内容
 */
const setCache = (key, value) => {
  try {
    uni.setStorageSync(key, value)
  } catch (e) {
    console.log(e)
  }
}
/**
 * getCache 同步获取缓存
 * @param {string} key key值
 */
const getCache = (key) => {
  try {
    var value = uni.getStorageSync(key)
    if (value) {
      return value;
    }
  } catch (e) {
    console.log(e)
    // Do something when catch error
  }
}
/**
 * removeCache 删除缓存
 * @param {string} key key值
 * @param {fun} successCallback 成功的回掉函数
 */
const removeCache = (key, successCallback) => {
  uni.removeStorage({
    key: key,
    success: successCallback
  })
}

/**
 * trim 去掉字符串前后空格
 * @param {string} str 需要去掉空格的字符串
 * @param {string} value 内容
 */
const trim = (str) => {
	return str.replace(/(^\s*)|(\s*$)/g, "")
}
/**
 * confirm 提示框
 * @param {string} title 标题
 * @param {string} content 内容
 * @param {Boolean}  showCancel 是否显示离开按钮
 * @param {fun} successCallback 成功的回掉函数
 */

const confirm = (content, successCallback, showCancel, title) => {
  uni.showModal({
    title: title || "提示",
    content: content,
    showCancel: showCancel || !1,
    success: successCallback,
    confirmColor: '#54B9EF'
  })
}

/**枚举EPlatform*/

/**使用条件编译获取平台信息*/
const ifDefPlatform = () => {
	
	var EPlatform =  {
	  /**App*/
	  AppPlus: 'APP-PLUS',
	  /**App nvue*/
	  AppPlusNvue: 'APP-PLUS-NVUE',
	  /**H5*/
	  H5: 'H5',
	  /**微信小程序*/
	  MpWeixin: 'MP-WEIXIN',
	  /**支付宝小程序*/
	  MpAlipay : 'MP-ALIPAY',
	  /**百度小程序*/
	  MpBaidu: 'MP-BAIDU',
	  /**字节跳动小程序*/
	  MpToutiao :'MP-TOUTIAO',
	  /**QQ小程序*/
	  MpQq :'MP-QQ',
	  /**360小程序*/
	  Mp360 :'MP-360',
	  /**微信小程序/支付宝小程序/百度小程序/字节跳动小程序/QQ小程序/360小程序*/
	  Mp : 'MP',
	  /**快应用通用(包含联盟、华为)*/
	  QuickappWebview : 'quickapp-webview',
	  /**快应用联盟*/
	  QuickappWebviewUnion : 'quickapp-webview-union',
	  /**快应用华为*/  
	  QuickappWebviewHuawei:'quickapp-webview-huawei',
	}

	  // let platform: EPlatform
	  //#ifdef APP-PLUS
	  platform = EPlatform.AppPlus;
	  //#endif
	  //#ifdef APP-PLUS-NVUE
	  platform = EPlatform.AppPlusNvue;
	  //#endif
	  //#ifdef H5
	  platform = EPlatform.H5;
	  //#endif
	  //#ifdef MP-WEIXIN
	  	 // console.log(22222);
	  platform = EPlatform.MpWeixin;
	  //#endif
	  //#ifdef MP-ALIPAY
	  platform = EPlatform.MpAlipay;
	  //#endif
	  //#ifdef MP-BAIDU
	  platform = EPlatform.MpBaidu;
	  //#endif
	  //#ifdef MP-TOUTIAO
	  platform = EPlatform.MpToutiao;
	  //#endif
	  //#ifdef MP-QQ
	  platform = EPlatform.MpQq;
	  //#endif
	  //#ifdef MP-360
	  platform = EPlatform.Mp360;
	  //#endif
	  //#ifdef quickapp-webview
	  platform = EPlatform.QuickappWebview;
	  //#endif
	  //#ifdef quickapp-webview-union
	  platform = EPlatform.QuickappWebviewUnion;
	  //#endif
	  //#ifdef quickapp-webview-huawei
	  platform = EPlatform.QuickappWebviewHuawei;
	  //#endif
	  return platform
}


module.exports = {
	confirm:confirm,
	trim:trim,
	toast: toast,
	getUrlParam:getUrlParam,
	regCard: regCard,
	regMobile: regMobile,
	regEmail: regEmail,
	loading: loading,
	hideLoading:hideLoading,
	openLogin: openLogin,
	replaceText: replaceText,
	gopage: gopage, //页面跳转不关闭当前页面
	gotopage: gotopage, //页面跳转关闭当前页面
	gotarpage: gotarpage, //页面跳转tarbar页面
	backpage: backpage, //页面返回
	alertBack: alertBack, //带弹出框的页面返回
	setCache: setCache, //添加缓存（同步）
	getCache: getCache, //获取缓存(同步)
	removeCache: removeCache ,//删除缓存
	ifDefPlatform:ifDefPlatform,
}
